package com.hcf.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hcf.project.common.convention.result.Result;
import com.hcf.project.common.convention.result.ResultTool;
import com.hcf.project.dto.req.ShortLinkCreateReqDTO;
import com.hcf.project.dto.req.ShortLinkPageReqDTO;
import com.hcf.project.dto.req.ShortLinkUpdateReqDTO;
import com.hcf.project.dto.resp.ListShortLinkCountRespDTO;
import com.hcf.project.dto.resp.ShortLinkCreateRespDTO;
import com.hcf.project.dto.resp.ShortLinkPageRespDTO;
import com.hcf.project.service.ShortLinkService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class ShortLinkController {

    private final ShortLinkService shortLinkService;

    @GetMapping("/{short-uri}")
    private void redirect(@PathVariable("short-uri") String shortUri, HttpServletRequest request, HttpServletResponse response){
        shortLinkService.redirect(shortUri, request, response);
    }

    @PostMapping("/api/shortlink/project/v1/create")
    private Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam){

        return ResultTool.success(shortLinkService.createShortLink(requestParam));
    }

    /**
     * 修改短链接
     * @param requestParam
     * @return
     */
    @PostMapping("/api/shortlink/project/v1/update")
    private Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam){
        shortLinkService.updateShortLink(requestParam);
        return ResultTool.success();
    }


    /**
     * 分页查询短链接
     * @param requestParam
     * @return
     */
    @GetMapping("/api/shortlink/project/v1/page")
    private Result<IPage<ShortLinkPageRespDTO>> pageShortLink(@RequestBody ShortLinkPageReqDTO requestParam){

        return ResultTool.success(shortLinkService.pageShortLink(requestParam));
    }


    /**
     * 查询短链接分组内数量
     * @param requestParam 分组id
     * @return
     */
    @GetMapping("/api/shortlink/project/v1/count")
    private Result<List<ListShortLinkCountRespDTO>> listShortLinkCount(@RequestParam("requestParam") List<String> requestParam){

        return ResultTool.success(shortLinkService.listShortLinkCount(requestParam));
    }
}
